package com.example.lanou.mynews.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.example.lanou.mynews.R;
import com.example.lanou.mynews.beans.AllDataBeans;
import com.example.lanou.mynews.beans.AllTitles;
import com.example.lanou.mynews.beans.PerTitle;
import com.example.lanou.mynews.db.dao.Titles;
import com.example.lanou.mynews.single.SingleDao;
import com.example.lanou.mynews.single.SingleMode;
import com.example.lanou.mynews.thread.AllTitleData;

/**
 * Created by dllo on 16/1/18.
 */
public class WaitRequest extends AppCompatActivity {
    private AllTitles titles;
    private AllDataBeans topics;
    private Handler handler = new Handler() {
        Bundle bundle = new Bundle();
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                titles = (AllTitles) msg.obj;
                for (int i=0;i<titles.gettList().size();i++) {
                    PerTitle per = titles.gettList().get(i);
                        int flag =1;
                    if(titles.gettList().size()/2<i)
                        flag=2;
                    SingleDao.getInstance().addTitle(new Titles(per.getTname(),per.getTemplate(),flag,per.getTid(),null));
                }
                bundle.putSerializable("titles",titles);
            }
            if (msg.what == 2) {
                topics = (AllDataBeans) msg.obj;
                bundle.putSerializable("topics",  topics);
            }
            if (((null != titles)&&(null != topics))||msg.what==3) {
             Intent intent = new Intent(WaitRequest.this, MainActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);
        ImageView iv = (ImageView) findViewById(R.id.activity_welcome_iv);
        AnimationDrawable animationDrawable = (AnimationDrawable) iv.getBackground();
        animationDrawable.start();
        AllTitleData thread = new AllTitleData(handler);
        thread.start();
    }
}
